package com.tms.pidan.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * 百度地图操作工具类
 */
public class BaiduMapUtils {
    private static final String AK = "94k6QfqF3lMhuw2xln4NqCibY5393t0h";

    public static void main(String[] args) {
        String origin = getCoordinate("北京市海淀区上地十街10号");
        String destination = getCoordinate("北京市天安门");

        Double distance = getDistance(origin, destination);
        Integer time = getTime(origin, destination);
        System.out.println("订单距离:" + distance + "米");
        System.out.println("线路耗时:" + time + "秒");
    }

    /**
     * 调用百度地图地理编码服务接口,根据地址获取坐标(经度,纬度)
     *
     * @param address
     * @return
     */
    public static String getCoordinate(String address) {
        String httpUrl = "https://api.map.baidu.com/geocoding/v3/?" +
                "address=" + address + "&output=json" + "&ak=" + AK;
        String json = loadJSON(httpUrl);

        Map map = JSON.parseObject(json, Map.class);

        String status = map.get("status").toString();
        if (status.equals("0")) {
            //返回结果成功,能够正常解析地址信息
            Map result = (Map) map.get("result");
            Map location = (Map) result.get("location");
            String lng = location.get("lng").toString();
            String lat = location.get("lat").toString();

            DecimalFormat df = new DecimalFormat("#.#####");
            String lngStr = df.format(Double.parseDouble(lng));
            String latStr = df.format(Double.parseDouble(lat));

            String rst = latStr + "," + lngStr; //纬度,经度

            return rst;
        }
        return null;
    }

    /**
     * 调用百度地图驾车路线规划服务接口,根据寄件人地址和收件人地址坐标计算订单距离/米
     *
     * @param origin
     * @param destination
     * @return
     */
    public static Double getDistance(String origin, String destination) {
        String httpUrl = "https://api.map.baidu.com/directionlite/v1/driving?" +
                "origin=" + origin +
                "&destination=" + destination + "&ak=" + AK;

        String json = loadJSON(httpUrl);

        Map map = JSON.parseObject(json, Map.class);
        if ("0".equals(map.getOrDefault("status", 500).toString())) {
            Map childMap = (Map) map.get("result");
            JSONArray jsonArray = (JSONArray) childMap.get("routes");
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            double distance = Double.parseDouble(jsonObject.get("distance") == null ? "0" : jsonObject.get("distance").toString());

            return distance;
        }
        return null;
    }

    /**
     * 调用百度地图驾车路线规划服务接口,根据寄件人地址和收件人地址坐标计算订单距离/秒
     *
     * @param origin
     * @param destination
     * @return
     */
    public static Integer getTime(String origin, String destination) {
        String httpUrl = "https://api.map.baidu.com/directionlite/v1/driving?" +
                "origin=" + origin +
                "&destination=" + destination + "&ak=" + AK;

        String json = loadJSON(httpUrl);

        Map map = JSON.parseObject(json, Map.class);
        if ("0".equals(map.getOrDefault("status", 500).toString())) {
            Map childMap = (Map) map.get("result");
            JSONArray jsonArray = (JSONArray) childMap.get("routes");
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            int time = Integer.parseInt(jsonObject.get("duration") == null ? "0" : jsonObject.get("duration").toString());

            return time;
        }
        return null;
    }

    /**
     * 调用服务接口,返回百度地图服务端的结果
     *
     * @param httpUrl
     * @return
     */
    public static String loadJSON(String httpUrl) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            URL url = new URL(httpUrl);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8));
            String inputLine = null;
            while ((inputLine = bufferedReader.readLine()) != null) {
                stringBuilder.append(inputLine);
            }
            bufferedReader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(stringBuilder.toString());
        return stringBuilder.toString();
    }
}
